//
//  NFKWebViewController.swift
//  NFKTicket
//
//  Created by 大洋 on 2022/9/25.
//

import UIKit
import WebKit

class NFKWebViewController: NFKBaseWebViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.baseWebView.uiDelegate = self
        self.baseWebView.navigationDelegate = self
        
        loadRequest()
    }

}

extension NFKWebViewController {
    func loadRequest() {
        requestFullURL = requestFullURL.lowercased()
        if requestFullURL.contains(" ") {
            requestFullURL = requestFullURL.addingPercentEncoding(withAllowedCharacters: .urlUserAllowed) ?? requestFullURL
        }
        
        if let url = URL(string: requestFullURL) {
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
            _ = self.baseWebView.load(request)
        }
    }
}


extension NFKWebViewController: WKUIDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(.allow)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print(#function)
    }
    
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(#function, error)
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(#function)
    }
}


extension NFKWebViewController: WKNavigationDelegate {
    
}

